package com.test.daoexample.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.test.daoexample.dao.UserDao;
import com.test.daoexample.pojo.User;
import com.test.daoexample.util.DBUtil;

public class UserDaoImpl implements UserDao {

	public int addUser(User user) {
		String sql = "insert into users(id, name,sex,sal,birth) values(idseq.nextval, ?,?,?,?)";
		Connection conn = null;
		PreparedStatement ps = null;
		int result = 0;
		
		try {
			conn = DBUtil.createConnection();
			ps = conn.prepareStatement(sql);
			
			ps.setString(1, user.getName());
			ps.setBoolean(2, user.isSex());
			ps.setDouble(3, user.getSal());
			ps.setDate(4, new java.sql.Date(user.getBirth().getTime()));
			
			result = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, null);
		}
		return result;
	}

	public int deleteUser(Integer id) {
		String sql = "delete from users where id=?";
		Connection conn = null;
		PreparedStatement ps = null;
		int result = 0;
		
		try {
			conn = DBUtil.createConnection();
			ps = conn.prepareStatement(sql);
			
			ps.setInt(1, id);
			
			result = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, null);
		}
		return result;
	}

	public int updateUser(User user) {
		String sql = "update users set name=?,sex=?,sal=?,birth=? where id=?";
		Connection conn = null;
		PreparedStatement ps = null;
		int result = 0;
		
		try {
			conn = DBUtil.createConnection();
			ps = conn.prepareStatement(sql);
			
			ps.setString(1, user.getName());
			ps.setBoolean(2, user.isSex());
			ps.setDouble(3, user.getSal());
			ps.setDate(4, new java.sql.Date(user.getBirth().getTime()));
			ps.setInt(5, user.getId());
			
			result = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, null);
		}
		return result;
	}

	public User loadUser(Integer id) {
		Connection conn = null;
		String sql = "select * from users where id=?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		User user = null;
		try {
			conn = DBUtil.createConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				user = new User(rs.getInt("id"), rs.getString("name"), rs.getBoolean("sex"), rs.getDouble("sal"), new Date(rs.getDate("birth").getTime()));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return user;
	}

	public List<User> findAll() {
		List<User> users = new ArrayList<User>();
		
		Connection conn = null;
		String sql = "select * from users";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = DBUtil.createConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				users.add(new User(rs.getInt("id"), rs.getString("name"), rs.getBoolean("sex"), rs.getDouble("sal"), new Date(rs.getDate("birth").getTime())));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		
		return users;
	}

}
